﻿var express = require('express');
var fs = require('fs');
var router = express.Router();
// init mongodb client
const {
    MongoClient
} = require('mongodb');
// connect url
const url = 'mongodb://localhost:27017';
// database name
const dbName = '1910';

/* GET users listing. */
router.get('/', function (req, res, next) {
    res.append('Access-Control-Allow-Origin', '*');
    // Use connect method to connect to the server
    MongoClient.connect(url, function (err, client) {
        if (err) throw err;
        // select db
        const db = client.db(dbName);
        // select collection
        const collection = db.collection('students');
        // Insert some documents
        collection.find({
            age: 18
        }).toArray((err, docs) => {
            if (err) throw err;
            res.json({
                docs
            });
            console.log(docs);
        })
        // chose connection
        client.close();
    })
});

// hey 老哥这个是我写的路由别动
router.get('/test', function (req, res, next) {

})

module.exports = router;